#!/usr/bin/env python3

import requests
import re
import json
import csv
from urllib.parse import urlencode
import datetime
import time
from config import *
import pymongo

#配置mongodb的信息
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_Field_NAME_DB]


def conn_microc_academic():
    headers = {
        'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
        'Host':'https://academic.microsoft.com/'
        }

    url = 'https://academic.microsoft.com/api/analytics/topics/hierarchy?topicPath='
    #url = 'https://academic.microsoft.com/api/analytics/topics/hierarchy?topicPath=71924100'
    #url = 'https://academic.microsoft.com/api/analytics/topics/hierarchy?topicPath=71924100%2C126322002'
    #url = 'https://academic.microsoft.com/api/analytics/topics/hierarchy?topicPath=71924100%2C126322002%2C170493617'
    try:
        response = requests.get(url)
        #print(response)
        if response.status_code == 200:
        #    print(111111111111111111)
        #    print(response.content)
            data_json = json.loads(response.content)
        #    print("--------------------------")
            print(data_json)
            return data_json['childTopics'],response.status_code
        else:
            return [],response.status_code

    except RequestException:
        return [],403


def main():
    data_json,response_status = conn_microc_academic()
    data_list_one = []
    if response_status == 200:
        for data_item in data_json:
            data_list_one.append({data_item.get("name"),data_item.get("id")})
            #db[MONGO_FIELD_NAME_COLLECTION].save({"name":data_item.get("name"),"name_id":data_item.get("id"),"ori_name":data_item.get("name"),"ori_name_id":data_item.get("id")})
        print(data_list_one)
    elif response_status == 403:
        print("获取数据失败")
    else:
        print("状态码出错")

if __name__ == '__main__':

    main()
